<?php
	/**
	 * CoreWebAjax
	 * @package Core
	 * @author Otavio Theiss - iperfly@gmail.com
	 * @copyright 15/08/2010
	 */
	class CoreWebAjax extends CoreFunctions {
	
		/**
		 * Gera um Javascript contendo o resultado do recordset
		 *
		 * @param recordset $rs
		 * @param string $name
		 * @param string $atributo
		 * @return string
		 */
		public function getJS(&$rs, $name = "recordset", $atributo = "row") {
			$rows = "";
			if (isset($rs)) {
				$rs->MoveFirst();
				while (!$rs->EOF) {
					$fields = $rs->GetRowAssoc();
					$row = "";
					foreach($fields as $key => $value) {
						$key = strtolower($key);
						$value = $this->noLineBreak($value);
						if ($row == "")
							$row .= " ".$key.": \"".$value."\"";
						else
							$row .= ", ".$key.": \"".$value."\"";
					}
					if ($rows == "")
						$rows .= "\n{ ".$row." }\n";
					else 
						$rows .= ",{ ".$row." }\n";
					$rs->MoveNext();
				}
				$rs->MoveFirst();
				return "var ".$name." = {\n".$atributo.": [".$rows."]\n};";
			}
		}
		
		/**
		 * Gera um documento XML contendo o resultado do recordset
		 *
		 * @param recordset $rs
		 * @param string $raiz
		 * @param string $no
		 * @param string $encoding
		 * @return string
		 */
		public function getXML(&$rs, $raiz = "recordset", $no = "row", $encoding = "ISO-8859-1") {
			$rows = "";
			if (isset($rs)) {
				$rs->MoveFirst();
				while (!$rs->EOF) {
					$fields = $rs->GetRowAssoc();
					$row = "";
					foreach($fields as $key => $value) {
						$key = strtolower($key);
						$row .= "\n<".$key.">".$value."</".$key.">";
					}
					$rows .= "\n<".$no.">".$row."\n</".$no.">";
					$rs->MoveNext();
				}
				$rs->MoveFirst();
			}
			return "<?xml version=\"1.0\" encoding=\"".$encoding."\"?>\n<".$raiz.">".$rows."\n</".$raiz.">";
		}
	}	
?>